package com.ts.flash.widget;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;

import com.ts.flash.Ledserver;
import com.ts.flash.R;

public class WidgetProvider extends AppWidgetProvider {
    private RemoteViews mRemoteViews = null;
    private static final String ACTION_LED_ON = "com.ts.flash.ACTION_LED_ON";
    private static final String ACTION_LED_OFF = "com.ts.flash.ACTION_LED_OFF";
    @Override
    public void onDeleted(Context context, int[] appWidgetIds) {
        super.onDeleted(context, appWidgetIds);
    }

    @Override
    public void onDisabled(Context context) {
        super.onDisabled(context);
    }

    @Override
    public void onEnabled(Context context) {
        super.onEnabled(context);
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        String action = intent.getAction();
        final Intent intentServer = new Intent(context, Ledserver.class);
        if (mRemoteViews == null) {
            mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
        }
        if (ACTION_LED_ON.equals(action)) {
            Intent intentOFF = new Intent(ACTION_LED_OFF);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1, intentOFF, PendingIntent.FLAG_CANCEL_CURRENT);
            mRemoteViews.setOnClickPendingIntent(R.id.widget_led, pendingIntent);
            mRemoteViews.setImageViewResource(R.id.widget_led, R.drawable.bg_on);

            context.startService(intentServer);
        } else if (ACTION_LED_OFF.equals(action)) {
            Intent intentON = new Intent(ACTION_LED_ON);
            PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1, intentON, PendingIntent.FLAG_CANCEL_CURRENT);
            mRemoteViews.setOnClickPendingIntent(R.id.widget_led, pendingIntent);
            mRemoteViews.setImageViewResource(R.id.widget_led, R.drawable.bg_off);

            context.stopService(intentServer);
        }
        AppWidgetManager appWidgetManger = AppWidgetManager.getInstance(context);
        int[] appIds = appWidgetManger.getAppWidgetIds(new ComponentName(context, WidgetProvider.class));
        appWidgetManger.updateAppWidget(appIds, mRemoteViews);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        super.onUpdate(context, appWidgetManager, appWidgetIds);
        if (mRemoteViews == null) {
            mRemoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
        }
        // 初始化widget并为imageView绑定事件
        Intent intent = new Intent(ACTION_LED_ON);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(context, -1, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.widget_led, pendingIntent);
        mRemoteViews.setImageViewResource(R.id.widget_led, R.drawable.bg_off);
        appWidgetManager.updateAppWidget(appWidgetIds, mRemoteViews);
    }
}
